<template>
  <div class="address-container">
    <div>
      <div class="card" v-for="item in addressList" :key="item.id">
        <ul class="selected">
          <li class="title" @click="onClickSelect(item.id)">
            <strong>{{ item.name }}</strong>&nbsp;
            <strong>{{ item.phone }}</strong>
          </li>
          <li @click="onClickSelect(item.id)">{{ item.area }} {{ item.detail }}</li>
          <li>
            <router-link class="edit" :to="{name: 'address_edit', params: {id: item.id}}">编辑</router-link>
          </li>
        </ul>
      </div>
    </div>
    <div v-show="showAdd" class="mod_btns fixed">
      <router-link :to="{ name: 'address_add' }" class="mod_btn">新增收货地址</router-link>
    </div>
  </div>
</template>

<script>
import {HTTPGetAddressList} from "@/api/address.js"
export default {
  data () {
    return {
      addressList: [],
      showAdd: false
    }
  },
  created () {
    this.getAddressList()
  },
  methods: {
    async getAddressList () {
      try {
        let result = await HTTPGetAddressList(); //获取登录用户的收货地址
        this.showAdd = true
        if (result.code === 1) {
          this.addressList = result.data; 
          // console.log(this.addressList) 
        }
        else if(result.code === 2){
          this.$router.push({ name: 'login' })
        }
        else{
          throw result.msg;
        }

      } catch (error) {
        this.$messagebox("错误",error)
      }
    },//================================================
    onClickSelect (addressId) {      
      if (this.$route.name == 'address_select') {
        this.$router.replace({ name: 'order_create', params: { addressId } })
      }      
    }
  }
}
</script>

<style lang="scss" scoped>
.address-container {
  background: #eee;
  overflow: hidden;
  .mod_btn {
    font-size: 16px;
    line-height: 46px;
    height: 46px;
    text-align: center;
    background: #3884ff;
    flex: 1;
    display: block;
    color: #fff;
  }
  .mod_btns .fixed {
    display: flex;
    overflow: hidden;
  }
  .card {
    padding: 7px 40px;
    position: relative;
    overflow: hidden;
    padding: 15px;
    color: #666;
    .selected {
      margin: 0;
      padding: 0;
      list-style: none;
      padding-right: 50px;
      .title {
        font-size: 16px;
      }
      .edit {
        color: #e93b3d;
        width: 50px;
        position: absolute;
        top: 50%;
        right: 0;
        transform: translateY(-50%);
      }
    }
  }
}
</style>
